package cn.edu.hhu.teacher.dao;

import cn.edu.hhu.teacher.bean.Teacher;
import cn.edu.hhu.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class TeacherDao {
    private final JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    public Teacher queryTeacherByIdAndPassword(String tid, String pwd) {
        String sql = "select * from teacher_info where tid = ? and pwd = ?";
        try {
            return template.queryForObject(sql, new BeanPropertyRowMapper<>(Teacher.class), tid, pwd);
        } catch (DataAccessException e) {
            return null;
        }
    }

    public Teacher queryTeacherByIdAndName(String tid, String tname) {
        String sql = "select * from teacher_info where tid = ? and tname = ?";
        try {
            return template.queryForObject(sql, new BeanPropertyRowMapper<>(Teacher.class), tid, tname);
        } catch (DataAccessException e) {
            return null;
        }
    }

    public int updateTeacherById(String tid,String pwd,String department,boolean activation) {
        String sql = "update teacher_info set pwd = ?, department = ?, activation = ? where tid = ?";
        try {
            return template.update(sql, pwd, department, activation, tid);
        } catch (DataAccessException e) {
            return 0;
        }
    }
}
